<?php

/**
 * XHTML_Label, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Label class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML__Inline_Forms.php";
include_once "XHTML_Inline.php";


/**
 * The XHTML_Label class is a handler for the label xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Label extends XHTML_Common_Element implements XHTML__Inline_Forms {
    
    // xhtml attributes
    
    /**
     * Id of the form element this label is for.
     * 
     * @var string
     */
    protected $for;
    
    /**
     * Accesibility key character for the label.
     *
     * @var string
     */
    protected $accessKey;
    
    /**
     * Script for the onfocus event.
     *
     * @var string
     */
    protected $onFocusScript;
    
    /**
     * Script for the onblur event.
     *
     * @var string
     */
    protected $onBlurScript;
    
    // inner elements
    
    /**
     * XHTML_Inline elements the label element has in its content.
     *
     * @var array
     */
    protected $elements;
    
      
    // Methods
    
    /**
     * Constructs a XHTML_Label object.
     */
    function __construct()
    {
        settype($this->elements, "array");
    }
    
    /**
     * Sets the id of the form element this label is for.
     * 
     * @param string $for
     */
    function setFor($for)
    {
        $this->for = $for;
    }
    
    /**
     * Gets the id of the form element this label is for.
     *
     * @return string
     */
    function getFor()
    {
        return $this->for;
    }
    
    /**
     * Sets the accesibility key character for the label.
     *
     * @param string $accessKey
     */
    function setAccessKey($accessKey)
    {
        $this->accessKey = $accessKey;
    }
    
    /**
     * Gets the accesibility key character for the label.
     * 
     * @return string
     */
    function getAccessKey()
    {
        return $this->accessKey;
    }
    
    /**
     * Sets the script for the onfocus event.
     * 
     * @param string $script
     */
    function setOnFocus($script)
    {
        $this->onFocusScript = $script;
    }
    
    /**
     * Gets the script for the onfocus event.
     *
     * @return string
     */
    function getOnFocus()
    {
        return $this->onFocusScript;
    }
    
    /**
     * Sets the script for the onblur event.
     * 
     * @param string $script
     */
    function setOnBlur($script)
    {
        $this->onBlurScript = $script;
    }
    
    /**
     * Gets the script for the onblur event.
     *
     * @return string
     */
    function getOnBlur()
    {
        return $this->onBlurScript;
    }
 
    /**
     * Sets the XHTML_Inline elements array.
     *
     * @param array
     */
    function setElements($elements) {
        $this->elements = $elements;
    }
    
    /**
     * Gets the XHTML_Inline elements array.
     *
     * @return array
     */
    function getElements()
    {
        return $this->elements;
    }
    
    /**
     * Adds a XHTML_Inline element to the inner array of this object.
     * 
     * @param XHTML_Inline
     */
    function addElement(XHTML_Inline $element)
    {
        array_push($this->elements, $element);
    }
    
    /**
     * Removes a XHTML_Inline element from the inner array of this object.
     * 
     * @param XHTML_Inline
     */
    function removeElement(XHTML_Inline $element)
    {
        if (($res = array_search($element, $this->elements)) !== false) {
            unset($this->element[$res]);
        }
    }

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<label".parent::toString();
        
        if (isset($this->for)) {
            $auxString = $auxString."for=\"".$this->for."\" ";
        }
        
        if (isset($this->accessKey)) {
            $auxString = $auxString."acceskey=\"".$this->accessKey."\" ";
        }
        
        if (isset($this->onFocusScript)) {
            $auxString = $auxString."onfocus=\"".$this->onFocusScript."\" ";
        }
        
        if (isset($this->onBlurScript)) {
            $auxString = $auxString."onblur=\"".$this->onBlurScript."\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Additions of the inner elements of the label element to the return 
         * string.
         */
        if (count($this->elements) > 0) {
            foreach ($this->elements as $element) {
                $auxString = $auxString.$element->toString();
            }
        }
        
        $auxString = $auxString."</label>\n";
        
        return $auxString;
    }
    
}

?>